package com.aal.model;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hoang
 */
public class Cart {

    private List<ProductInCart> items = new ArrayList<ProductInCart>();

    public void setItemsReset(boolean junk) {
        items = new ArrayList<ProductInCart>();
    }
    /**
     * Get current items in cart
     *
     * @return the value of items
     */
    public List<ProductInCart> getItems() {
        return items;
    }
    
    public int getTotalItems() {
        int totalItems = 0;
        
        if (items == null) return 0;
        
        for (ProductInCart item : items) {
            totalItems += item.getQuantity();
        }
        
        return totalItems;
    }
    
    public double getTotalAmount() {
        double amount = 0;
        
        if (items == null) return 0;
        
        amount = getNetAmount() + getShippingAmount() + getTaxAmount() - getDiscountAmount();
        
        return amount;
    }
    
    public double getNetAmount() {
        double amount = 0;
        if (items == null) return 0;
        for (ProductInCart item : items) {
            amount += item.getNetAmount();
        }
        
        return amount;
    }
    
    public double getShippingAmount() {
        double amount = 0;
        if (items == null) return 0;
        for (ProductInCart item : items) {
            amount += item.getShippingAmount();
        }
        
        return amount;
    }
    
    public double getTaxAmount() {
        double amount = 0;
        if (items == null) return 0;
        for (ProductInCart item : items) {
            amount += item.getTaxAmount();
        }
        
        return amount;
    }
    

    public double getDiscountAmount() {
        double amount = 0;
        
        if (items == null) return 0;
        
        amount = getNetAmount() * discountPercent();
        
        return amount;
    }

    private int customerGroupID = 1;

    /**
     * Set the value of customerGroupID
     *
     * @param customerGroupID new value of customerGroupID
     */
    public void setCustomerGroupID(int customerGroupID) {
        this.customerGroupID = customerGroupID;
    }

    /*
    public double getDiscountAmount() {
        return quantity * product.getUnitPrice() * discountPercent();
    }
     * 
     */
    
    private double discountPercent() {
        CustomerManagement cm = new CustomerManagement();

        cm.setCustomergroupId(customerGroupID);
        
        return cm.getDiscountCustomerGroup();
    }
    
    public void setAddItemToCart(int productID) {
        boolean existed = false;
        
        if (items != null)
        // increase quantity if item already exists
        for (ProductInCart item : items) {
            if (item.getProduct().getProductID() == productID) {
                item.setQuantity(quantity + item.getQuantity());
                existed = true;
                break;
            }
        }
        
        // create new item if not exists
        if (!existed) {
            ProductInCart item = new ProductInCart();
            ProductManagement pm = new ProductManagement();
            item.setProduct(pm.getProductbyid(productID));
            item.setQuantity(quantity);
            items.add(item);
        }
        
    }
    
    public void setRemoveItemFromCart(int productID) {
        for (ProductInCart item : items) {
            if (item.getProduct().getProductID() == productID) {
                items.remove(item);
                break;
            }
        }
    }
    
    private int quantity;

    /**
     * Set the value of quantity
     *
     * @param quantity new value of quantity
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    
    public void setEditItemInCart(int productID) {
        // remove if quantity <= 0
        if (quantity < 1)
            setRemoveItemFromCart(productID);
        
        // set new quantity if item exists
        for (ProductInCart item : items) {
            if (item.getProduct().getProductID() == productID) {
                item.setQuantity(quantity);
                break;
            }
        }
    }


}
